#第七节 pyqt5控件1
import sys,os
from PyQt5.QtWidgets import (QApplication,QCheckBox,QComboBox,
QSlider,QSlider,QLabel,QProgressBar,QCalendarWidget,QPushButton)
from PyQt5.QtCore import Qt,QTimer,QBasicTimer,QDate
from PyQt5.QtGui import QPixmap


from base.CommonWin import BaseMainWin

class MyMainWin(BaseMainWin):
    def __init__(self,title='我的QT', sizeW=500, sizeH=500, icoUrl='assets/ico/logo.png'):
        super().__init__(title,sizeW,sizeH,icoUrl)

    def initUI(self):
        super().initUI()
        self.cb = QCheckBox("是否自动选择",self)        
        self.cb.stateChanged.connect(self.cbChange)
        self.cb.toggle() #开关切换
        self.cb.move(30,10)
        
        #滑块组件
        self.slider = QSlider(Qt.Horizontal,self)
        self.slider.setGeometry(30,40,100,30)
        self.slider.setMaximum(100)
        self.slider.valueChanged[int].connect(self.sliderChange)

        self.proBar = QProgressBar(self)
        self.proBar.setMaximum(100)
        self.proBar.setGeometry(self.slider.x(),self.slider.y()+self.slider.height()+10,200,25)
        self.proBar.setValue(0)
        
        self.comBox = QComboBox(self)
        self.comBox.setGeometry(self.proBar.x(),self.proBar.y()+self.proBar.height()+10,150,30)
        self.comBox.addItems(["BaseModule","BaseWindow","BaseTabPanel","ListRenderer"])
        self.comBox.activated[str].connect(self.oncomBoxChange)

        self.timer = QBasicTimer()        
        self.step = 0

        

        self.startBtn = QPushButton("开始",self)
        self.startBtn.move(self.proBar.x()+self.proBar.width()+10,self.proBar.y())
        self.startBtn.clicked.connect(self.doAction)
    def oncomBoxChange(self):
        print("currentData",self.comBox.currentData())
        print("currentData",self.comBox.currentIndex())
        print("currentText",self.comBox.currentText())
    def cbChange(self,state):
        '''
        checkbox状态改变的处理函数
        '''
        print('cbChange state',self.cb.isChecked(),state) #state 是一个整数值

    def sliderChange(self,value):
        """拖动条变化
        
        Arguments:
            value {[int]} -- [当前值]
        """
        print('value',value)
        #self.proBar.setValue(value)

    def doAction(self):
        if self.timer.isActive():
            self.timer.stop()
            self.step = 0
            self.proBar.setValue(self.step)
            self.startBtn.setText('开始')
        else:
            self.timer.start(100,self) #100毫秒
            self.startBtn.setText('结束')


    #每个QObject及其子类都有一个timerEvent()事件处理器。我们要重新实现这个事件处理器来响应定时器事件。
    def timerEvent(self,e):        
        if self.step>=100:
            self.doAction()
        else:
            self.step = self.step + 1
            self.proBar.setValue(self.step)

    


    
if __name__ == "__main__":
    app = QApplication(sys.argv)
    
    w = MyMainWin()
    w.show()
    ROOT_PATH = os.getcwd()
    print("ROOT_PATH",ROOT_PATH)
    fileName = ROOT_PATH+"/assets/test.txt"
    print("ROOT_PATH",ROOT_PATH,fileName)
    with open(fileName,'r',encoding="utf8") as f:
        content = f.read()
        print("content",content)
    sys.exit(app.exec_())